var yToolbars = new Array();
var YInitialized = false;
var charset="UTF-8";
function document.onreadystatechange() {
    if (YInitialized) return;
    YInitialized = true;
    var i, s, curr;
    for (i=0; i<document.body.all.length; i++) {
        curr=document.body.all[i];
        if (curr.className == "yToolbar") {
            InitTB(curr);
            yToolbars[yToolbars.length] = curr;
        }
    }
}

function InitBtn(btn) {
    btn.onmouseover = BtnMouseOver;
    btn.onmouseout = BtnMouseOut;
    btn.onmousedown = BtnMouseDown;
    btn.onmouseup = BtnMouseUp;
    btn.ondragstart = YCancelEvent;
    btn.onselectstart = YCancelEvent;
    btn.onselect = YCancelEvent;
    btn.YUSERONCLICK = btn.onclick;
    btn.onclick = YCancelEvent;
    btn.YINITIALIZED = true;
    return true;
}

function InitTB(y) {
    y.TBWidth = 0;
    if (!PopulateTB(y)) return false;
    y.style.posWidth = y.TBWidth;
    return true;
}

function YCancelEvent() {
    event.returnValue = false;
    event.cancelBubble = true;
    return false;
}

function BtnMouseOver() {
    if (event.srcElement.tagName != "IMG") return false;
    var image = event.srcElement;
    var element = image.parentElement;
    if (image.className == "Ico") element.className = "BtnMouseOverUp";
    else if (image.className == "IcoDown") element.className = "BtnMouseOverDown";
    event.cancelBubble = true;
}

function BtnMouseOut() {
    if (event.srcElement.tagName != "IMG") {
        event.cancelBubble = true;
        return false;
    }
    var image = event.srcElement;
    var element =   image.parentElement;
    yRaisedElement = null;
    element.className = "Btn";
    image.className = "Ico";
    event.cancelBubble = true;
}

function BtnMouseDown() {
    if (event.srcElement.tagName != "IMG") {
        event.cancelBubble = true;
        event.returnValue=false;
        return false;
    }
    var image = event.srcElement;
    var element = image.parentElement;
    element.className = "BtnMouseOverDown";
    image.className = "IcoDown";
    event.cancelBubble = true;
    event.returnValue=false;
    return false;
}

function BtnMouseUp() {
    if (event.srcElement.tagName != "IMG") {
        event.cancelBubble = true;
        return false;
    }
    var image = event.srcElement;
    var element = image.parentElement;
    if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
    element.className = "BtnMouseOverUp";
    image.className = "Ico";
    event.cancelBubble = true;
    return false;
}

function PopulateTB(y) {
    var i, elements, element;
    elements = y.children;
    for (i=0; i<elements.length; i++) {
    element = elements[i];
    if (element.tagName== "SCRIPT" || element.tagName == "!") continue;
    switch (element.className) {
        case "Btn":
            if (element.YINITIALIZED == null) {
                if (! InitBtn(element))
                    return false;
            }
            element.style.posLeft = y.TBWidth;
            y.TBWidth   += element.offsetWidth + 1;
            break;
        case "TBGen":
            element.style.posLeft = y.TBWidth;
            y.TBWidth   += element.offsetWidth + 1;
            break;
        }
    }
    y.TBWidth += 1;
    return true;
}

function DebugObject(obj) {
    var msg = "";
    for (var i in TB) {
        ans=prompt(i+"="+TB[i]+"\n");
        if (! ans) break;
    }
}

function format1(what,opt) {
    if (opt=="removeFormat") {
        what=opt;
        opt=null;
    }
    composition.focus();
    if (opt==null) {
        composition.document.execCommand(what);
    } else {
        composition.document.execCommand(what,"",opt);
    }
    composition.focus();
}

function format(what,opt) {
    format1(what,opt);
}

function setStyle() {
    bs = composition.document.body.runtimeStyle;
    bs.fontSize = "10pt";
    bs.scrollbar3dLightColor = "#D4D0C8";
    bs.scrollbarArrowColor = "#000000";
    bs.scrollbarBaseColor = "#D4D0C8";
    bs.scrollbarDarkShadowColor = "#D4D0C8";
    bs.scrollbarFaceColor = "#D4D0C8";
    bs.scrollbarHighlightColor = "#808080";
    bs.scrollbarShadowColor = "#808080";
    bs.scrollbarTrackColor = "#D4D0C8";
    bs.border = "0";
}

function UserDialog(what) {
    composition.focus();
    composition.document.execCommand(what, true);
    if(what=="InsertImage") {
        composition.document.body.innerHTML=(composition.document.body.innerHTML).replace("src=\"file://","src=\"");
    }
    composition.focus();
}

function foreColor() {
    var arr = showModalDialog("selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
    if (arr != null) format('forecolor', arr);
    else composition.focus();
}

function backColor() {
    var arr = showModalDialog("selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
    if (arr != null) format('backcolor', arr);
    else composition.focus();
}

function CleanCode() {
    editor=composition;
    editor.focus();
    var body = editor.document.body;
    for (var index = 0; index < body.all.length; index++) {
        tag = body.all[index];
        tag.removeAttribute("className","",0);
        tag.removeAttribute("style","",0);
    }
    var html = editor.document.body.innerHTML;
    html = html.replace(/<o:p>&nbsp;<\/o:p>/gi, "");
    html = html.replace(/o:/gi, "");
    html = html.replace(/<font[^>]*>\s*<\/font>/gi, "");
    html = html.replace(/<span>\s*<\/span>/gi, "");
    editor.document.body.innerHTML = html;
}

function cleanAllHtm() {
    editor=composition;
    editor.focus();
    var body = editor.document.body;
    for (var index = 0; index < body.all.length; index++) {
        tag = body.all[index];
        tag.removeAttribute("className","",0);
        tag.removeAttribute("style","",0);
    }
    var html = editor.document.body.innerHTML;
    html = html.replace(/\<[^>]*>/g,""); 
    editor.document.body.innerHTML = html;
}

function cleanHtml() {
    var fonts = composition.document.body.all.tags("FONT");
    var curr;
    for (var i = fonts.length - 1; i >= 0; i--) {
        curr = fonts[i];
        if (curr.style.backgroundColor == "#ffffff") curr.outerHTML = curr.innerHTML;
    }
}

function correctUrl(cont) {
    var url=location.href.substring(0,location.href.lastIndexOf("/")+1);
    cont=rCode(cont,location.href+"#","#");
    cont=rCode(cont,url,"");
    cont=rCode(cont,"<a>..</a>","");
    var r = new RegExp('(a href=[^>)]*)','gi');
    cont=cont.replace(r,"$1 target=_blank");
    //var r = new RegExp('(target=_blank target=_blank)','gi');
    cont=cont.replace("target=_blank target=_blank","target=_blank");
    return cont;
}

function InitDocument(hiddenid) {
    var bodyTag="<html><head><style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}</style></head><BODY bgcolor=\"#FFFFFF\" >";
    var editor=composition;
    var h=document.getElementById(hiddenid);
    editor.document.designMode="On"
    editor.document.open();
    editor.document.write(bodyTag);
    if (h.value!="") {
        editor.document.write(h.value);
    }
    editor.document.write("</html>");
    editor.document.close();
    editor.document.body.contentEditable = "True";
    editor.document.charset=charset;
    setStyle();
}

function rCode(s,a,b,i) {
    a = a.replace("?","\\?");
    if (i==null) {
        var r = new RegExp(a,"gi");
    }else if (i) {
        var r = new RegExp(a,"g");
    }else{
        var r = new RegExp(a,"gi");
    }
    return s.replace(r,b); 
}

function copyData(hiddenid) {
    d = composition.document;
    cont=d.body.innerHTML;
    cont=correctUrl(cont);
    document.getElementById(hiddenid).value = cont;
    if (document.getElementById(hiddenid).value == '<P>&nbsp;</P>') {
        document.getElementById(hiddenid).value = '';
    }
}

function getHTML() {
    var html;
    html = composition.document.body.innerHTML
    return html;
}

function getFocus() {
    if ( composition)
        composition.focus();
}

function insertHtml(str) {
    var sel = composition.document.selection.createRange();
    sel.pasteHTML(str);
}

function htmlEncode(text){
    text = text.replace(/&/g, "&amp;") ;
    text = text.replace(/"/g, "&quot;") ;
    text = text.replace(/</g, "&lt;") ;
    text = text.replace(/>/g, "&gt;") ;
    text = text.replace(/'/g, "&#146;") ;
    text = text.replace(/\ /g,"&nbsp;");
    text = text.replace(/\n/g,"<br/>");
    text = text.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
    return text;
}
